package com.hongyuan.venue.base;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.databinding.BindingAdapter;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.hongyuan.venue.R;
import com.hongyuan.venue.data.md5.BaseUtil;
import com.hongyuan.venue.myview.VersionChangeView;
import com.hongyuan.venue.myview.risenumber_textview.RiseNumberTextView;
import com.hongyuan.venue.ui.main.beans.CheckVersionBeans;
import com.luck.picture.lib.tools.ScreenUtils;
import com.makeramen.roundedimageview.RoundedImageView;
import com.zhouwei.mzbanner.MZBannerView;
import com.zhouwei.mzbanner.holder.MZHolderCreator;
import com.zhouwei.mzbanner.holder.MZViewHolder;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class MyBindingAdapter {

    @BindingAdapter(value = {"imageUrl", "defaultImg"}, requireAll = false)
    public static void bindImageUrl(ImageView view, Object imageUrl, Drawable defaultImg){
        if(BaseUtil.isValue(imageUrl)){
            Glide.with(view.getContext()).load(imageUrl).into(view);
        }else{
            if(defaultImg != null){
                Glide.with(view.getContext()).load(defaultImg).into(view);
            }else{
                Glide.with(view.getContext()).load(R.mipmap.fang_default).into(view);
            }
        }
    }

    @BindingAdapter("imageFile")
    public static void bindImageUrl(ImageView view, File imageFile){
        if(!BaseUtil.isValue(imageFile))return;

        Glide.with(view.getContext()).load(imageFile).into(view);
    }


    @BindingAdapter("intImg")
    public static void bindIntImg(ImageView img,int imgId){
        if(imgId != 0){
            img.setImageResource(imgId);
        }else{
            img.setImageResource(R.color.theme_FFFFFF);
        }

    }

    @BindingAdapter("setBanner")
    public static void bindBannerUrl(MZBannerView view, List<String> imgs){
        if(imgs == null)return;

        view.setPages(imgs, (MZHolderCreator<BannerViewHolder>) () -> new BannerViewHolder());

        view.start();
    }

    public static class BannerViewHolder implements MZViewHolder<String> {
        private ImageView mImageView;
        @Override
        public View createView(Context context) {
            // 返回页面布局
            View view = LayoutInflater.from(context).inflate(R.layout.item_banner,null);
            mImageView = view.findViewById(R.id.img);
            return view;
        }

        @Override
        public void onBind(Context context, int position, String data) {

            RequestOptions options = new RequestOptions().placeholder(R.drawable.long_default).error(R.drawable.long_default);
            Glide.with(context).load(data).apply(options).into(mImageView);
        }
    }



    @BindingAdapter("setIntText")
    public static void bindPraiseImg(TextView textView, int nums){
        textView.setText(String.valueOf(nums));
    }

    @BindingAdapter("setAppChangeDate")
    public static void bindUpdateAppData(VersionChangeView view, CheckVersionBeans.InfoBean infoBean){
        if(infoBean != null && infoBean.getIs_new() == 1){
            view.startChange(infoBean);
        }else{
            return;
        }
    }

    @BindingAdapter("setIntBg")
    public static void bindViwBg(View view, int bgId){
        view.setBackgroundResource(bgId);
    }

    @BindingAdapter("setMyTextColor")
    public static void bindTextColorBg(TextView view, int bgId){
        view.setTextColor(view.getResources().getColor(bgId));
    }


    @BindingAdapter("setRNTextNum")
    public static void bindRiseNumTextView(RiseNumberTextView view, String num){
        if(Float.parseFloat(num) <= 0){
            return;
        }
        view.withNumber(Float.parseFloat(num)).start();
    }


    @BindingAdapter("setRadioGroupCheck")
    public static void bindRadioGroupCheck(RadioGroup view, int checkId){
        if(checkId == -1){
            view.clearCheck();
        }else{
            view.check(checkId);
        }
    }

    @BindingAdapter("setWebContent")
    public static void bindWebView(WebView view, String des){
        if(!BaseUtil.isValue(des))return;

        view.loadDataWithBaseURL(null, getNewData(view.getContext(),des),"text/html", "utf-8",null);
    }


    /**
     * 设置img标签下的width为手机屏幕宽度，height自适应
     *
     * @param data html字符串
     * @return 更新宽高属性后的html字符串
     */
    private static String getNewData(Context context, String data) {
        Document document = Jsoup.parse(data);

        Elements pElements = document.select("p:has(img)");
        for (Element pElement : pElements) {
            pElement.attr("style", "text-align:center");
            pElement.attr("max-width", ScreenUtils.getScreenWidth(context) + "px")
                    .attr("height", "auto");
        }
        Elements imgElements = document.select("img");
        for (Element imgElement : imgElements) {
            //重新设置宽高
            imgElement.attr("max-width", "100%")
                    .attr("height", "auto");
            imgElement.attr("style", "max-width:100%;height:auto");
        }
        return document.toString();
    }
}
